/*
 * Copyright 2012 Marsrutai.lt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.task;

import java.util.List;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.activity.intent.TaskIntentHolder;
import lt.marsrutai.mobile.android.exception.DataAccessException;
import lt.marsrutai.mobile.android.model.ArrivalTime;
import lt.marsrutai.mobile.android.model.enums.DateTime;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Parcel;
import android.os.Parcelable;

/**
 *
 * @author Justas
 */
public class GetArrivalTimesTask extends NotifyingAsyncTask<GetArrivalTimesTask.Criteria, Void, List<ArrivalTime>> {

    public static final String BROADCAST_ACTION = "lt.marsrutai.mobile.android.task.GetArrivalTimesTask";

    @Override
    protected List<ArrivalTime> doInBackground(Criteria... params) {
        if (params.length != 1) {
            throw new IllegalArgumentException("Only one criteria must be provided for GetArrivalTimesTask.");
        }

        Criteria criteria = params[0];
        try {
            ConnectivityManager connManager = (ConnectivityManager)AppContext.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            boolean isConnected = networkInfo != null && networkInfo.isConnected();

            return AppContext.getDataService().getArrivalTimes(criteria.getScopeFolder(), criteria.getStopId(), null,
                    criteria.getScheduleId(), criteria.getMaxMinutes(), criteria.getMaxResults(), new DateTime().formatFull(), isConnected);
        }
        catch (DataAccessException e) {
            setResultStatus(RESULT_NOT_RESOLVED);
        }
        return null;
    }

    @Override
    protected Intent getResultIntent(List<ArrivalTime> result) {
        TaskIntentHolder intentHolder = new TaskIntentHolder(super.getResultIntent(result));
        intentHolder.setResult(result, ArrivalTime.class);
        return intentHolder.getIntent();
    }

    @Override
    protected String getBroadcastAction() {
        return BROADCAST_ACTION;
    }

    public static class Criteria implements Parcelable {

        public static final Parcelable.Creator<Criteria> CREATOR = new Parcelable.Creator<Criteria>() {

            @Override
            public Criteria createFromParcel(Parcel source) {
                return new Criteria(source);
            }

            @Override
            public Criteria[] newArray(int size) {
                return new Criteria[size];
            }
        };

        private String scopeFolder;
        private String stopId;
        private String stopName;
        private String scheduleId;
        private int maxMinutes;
        private int maxResults;

        public Criteria() {
        }

        public Criteria(String scopeName, String stopName, String stopId, String scheduleId, int maxMinutes, int maxResults) {
            this.scopeFolder = scopeName;
            this.stopName = stopName;
            this.stopId = stopId;
            this.scheduleId = scheduleId;
            this.maxMinutes = maxMinutes;
            this.maxResults = maxResults;
        }

        public Criteria(Parcel in) {
            scopeFolder = in.readString();
            stopName = in.readString();
            stopId = in.readString();
            scheduleId = in.readString();
            maxMinutes = in.readInt();
            maxResults = in.readInt();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(scopeFolder);
            dest.writeString(stopName);
            dest.writeString(stopId);
            dest.writeString(scheduleId);
            dest.writeInt(maxMinutes);
            dest.writeInt(maxResults);
        }

        @Override
        public int describeContents() {
            return 0;
        }

        public String getScopeFolder() {
            return scopeFolder;
        }

        public void setScopeFolder(String scopeFolder) {
            this.scopeFolder = scopeFolder;
        }

        public String getStopId() {
            return stopId;
        }

        public String getStopName() {
            return stopName;
        }

        public void setStopName(String stopName) {
            this.stopName = stopName;
        }

        public void setStopId(String stopId) {
            this.stopId = stopId;
        }

        public int getMaxMinutes() {
            return maxMinutes;
        }

        public void setMaxMinutes(int maxMinutes) {
            this.maxMinutes = maxMinutes;
        }

        public int getMaxResults() {
            return maxResults;
        }

        public void setMaxResults(int maxResults) {
            this.maxResults = maxResults;
        }

        public String getScheduleId() {
            return scheduleId;
        }

        public void setScheduleId(String scheduleId) {
            this.scheduleId = scheduleId;
        }

    }

}
